<?php
/**
 * Fastapp.
 * 1/11/25
 * @Author deepsea
 * @Contact telegram:deepsea159
 */


namespace App\Fastapp\Api\Service\Cms;


use App\Fastapp\Api\Service\BaseService;
use App\Fastapp\Model\FastappCategory;
use Mine\MineCollection;

class CategoryService extends BaseService
{
    public function getByCode(string $code): array
    {
        $list = FastappCategory::query()->select(['id', 'name', 'icon', 'num', 'view', 'code'])
            ->where(['status' => 1])
            ->whereIn('code', explode(',', $code))
            ->orderByDesc('sort')
            ->get()->map(function ($item) {
                $item->name = fastapp_lang_text($item->name);
                return $item;
            });
        return ['list' => $list];
    }

    public function tree(string $code): array
    {
        $treeData = FastappCategory::query()
            ->select(['id', 'name', 'icon', 'view', 'num', 'parent_id'])
            ->where(['status' => 1])
            ->whereIn('code', explode(',', $code))
            ->orderByDesc('sort')
            ->get()->map(function ($item) {
                $item->name = fastapp_lang_text($item->name);
                return $item;
            });
        $tree = (new MineCollection())->toTree($treeData->toArray());
        return ['tree' => $tree];

    }
}